package cn.bdqn.service.impl;

import cn.bdqn.domain.SendEquip;
import cn.bdqn.domain.SendFreight;
import cn.bdqn.mapper.SendEquipMapper;
import cn.bdqn.mapper.SendFreightMapper;
import cn.bdqn.service.SendFreightService;
import cn.bdqn.utils.result.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 发货单业务接口
 * @author dkj
 */
@Service
public class SendFreightServiceImpl implements SendFreightService {


    @Autowired
    private SendFreightMapper sendFreightMapper;

    @Autowired
    private SendEquipMapper sendEquipMapper;
    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public SendFreight queryByPrimaryKey(Integer id) {

        //根据id查询
        SendFreight sendFreight = sendFreightMapper.selectByPrimaryKey(id);
        //根据发货单id查询
        List<SendEquip> sendEquips = sendEquipMapper.selectBySendFreightId(id);
        //赋值
        sendFreight.setSendEquips(sendEquips);
        //返回结果
        return sendFreight;
    }

    /**
     * 根据发货单号模糊查询
     * @param sendFreightNumber
     * @return
     */
    @Override
    public List<SendFreight> queryByLikeSendFreightNumber(String sendFreightNumber) {

        //根据发货单号模糊查询
        return  sendFreightMapper.selectByLikeSendFreightNumber(sendFreightNumber);

    }

    /**
     * 根据发货订单和支付类型和发货单状态查询
     * @param currentPage
     * @param pageSize
     * @param sendFreightNumber
     * @param paymentType
     * @param sendFreightState
     * @return
     */
    @Override
    public Page queryLikeSendFreightNumberByPaymentTypeBySendFreightState(Integer currentPage, Integer pageSize, String sendFreightNumber, Integer paymentType, Integer sendFreightState) {

        //开启分页
        PageHelper.startPage(currentPage>0?currentPage:1,pageSize);
        //查询
        List<SendFreight> sendFreights = sendFreightMapper
                .selectLikeSendFreightNumberByPaymentTypeBySendFreightState(sendFreightNumber, paymentType, sendFreightState);
        //分页
        PageInfo<SendFreight> sendFreightPageInfo = new PageInfo<>(sendFreights);
        //封装
        Page page = new Page();
        //数据
        page.setMenuList(sendFreightPageInfo.getList());
        //设置总数量
        page.setTotalRecord((int)sendFreightPageInfo.getTotal());
        //返回结果
        return page;
    }

    /**
     * 新增
     * @param sendFreight
     * @return
     */
    @Override
    public SendFreight saveSendFreight(SendFreight sendFreight) {

        //新增
        sendFreightMapper.insertSendFreight(sendFreight);
        //返回
        return sendFreight;
    }

    /**
     * 更新
     * @param sendFreight
     */
    @Override
    public void updateSendFreight(SendFreight sendFreight) {

        //更新
        sendFreightMapper.updateSendFreight(sendFreight);
    }
}
